import React, { useState, useEffect } from "react";

const Countdown = ( seconds, callback ) => {
  const [remainingSeconds, setRemainingSeconds] = useState(seconds);
  const [isRunning, setIsRunning] = useState(false);

  const startCountdown = () => {
    setIsRunning(true);
    setRemainingSeconds(seconds);
  };

  const resetCountdown = () => {
    setIsRunning(false);
    setRemainingSeconds(seconds);
  };

  useEffect(() => {
    if (remainingSeconds === 0) {
      setIsRunning(false);
      callback();
    }

    if (isRunning) {
      const interval = setInterval(() => {
        setRemainingSeconds(prevSeconds => prevSeconds - 1);
      }, 1000);

      return () => clearInterval(interval);
    }
  }, [remainingSeconds, callback, isRunning]);

  return [remainingSeconds,startCountdown,resetCountdown]
};

export default Countdown;